package com.zlsx.androiddemo.arp;

import android.util.Log;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * ARP工具类
 */
public class ARPUtil {
    private final int IntelnetPort=6555;
    private List<ArpBean> mArpBeanList = new ArrayList<>();
    private static final String TAG = ARPUtil.class.getName();

    private static class SingletonHolder {
        private static final ARPUtil INSTANCE = new ARPUtil();
    }

    private ARPUtil() {
    }

    public static ARPUtil getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void discover(String ip) {
        String newip = "";
        if (!ip.equals("")) {
            String ipseg = ip.substring(0, ip.lastIndexOf(".") + 1);
            for (int i = 2; i < 255; i++) {
                newip = ipseg + String.valueOf(i);
                if (newip.equals(ip)) continue;
                Thread ut = new UDPThread(newip);
                ut.start();
            }
        }
    }
    public void sendUDP(String info,String ip){
        try {
            DatagramSocket datagramSocket = new DatagramSocket();
            DatagramPacket datagramPacket=new DatagramPacket(info.getBytes(),info.getBytes().length, InetAddress.getByName(ip),IntelnetPort);
            datagramSocket.send(datagramPacket);
            datagramSocket.close();
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 读取Arp列表
     */
    public void readArp() {
        try {
            if(mArpBeanList!=null){
                mArpBeanList.clear();
            }else{
                mArpBeanList=new ArrayList<>();
            }

            BufferedReader br = new BufferedReader(
                    new FileReader("/proc/net/arp"));
            String line = "";
            String ip = "";
            String flag = "";
            String mac = "";

            while ((line = br.readLine()) != null) {

                try {
                    line = line.trim();
                    if (line.length() < 63) continue;
                    if (line.toUpperCase(Locale.US).contains("IP")) continue;
                    ip = line.substring(0, 17).trim();
                    flag = line.substring(29, 32).trim();
                    mac = line.substring(41, 63).trim();
                    if (mac.contains("00:00:00:00:00:00")) continue;
                    mArpBeanList.add(new ArpBean(ip, mac, flag));
                    Log.e(TAG, "readArp: mac= " + mac + " ; ip= " + ip + " ;flag= " + flag);
                } catch (Exception e) {
                }
            }
            br.close();

        } catch (Exception e) {
        }
    }
}
